package com.gibs.peixunapi.controller;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
import java.util.stream.Collectors;


import com.gibs.peixunapi.VO.UserVO;
import com.gibs.peixunapi.excepiton.BaseException;
import com.gibs.peixunapi.utils.ConverterUtil;
import io.swagger.annotations.*;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.annotation.*;

import lombok.extern.slf4j.Slf4j;
import com.gibs.peixunapi.enums.ResultEnum;
//import com.gibs.peixunapi.config.ConfigUtils;
//import com.gibs.peixunapi.controller.Application;
import javax.persistence.EntityNotFoundException;
import javax.transaction.Transactional;
import javax.validation.Valid;

import com.gibs.peixunapi.service.UserService;
import com.gibs.peixunapi.model.User;
import com.gibs.peixunapi.result.Result;
import com.gibs.peixunapi.utils.ResultUtil;

/**
 * 功能：【用户】的后台控制器
 * 项目：装配式培训平台
 * 日期：2020-08-20
 * 作者：梁嘉伟
 */

@Slf4j
@RestController
@Api(tags = "用户信息模块")
@RequestMapping("/Users")
public class Users extends Application {

    @Autowired
    private UserService userService;

    @Transactional
    @ApiOperation(value = "根据User对象新建用户")
//    @ApiImplicitParam(name = "object", value = "用户详细实体UserVO", required = true,paramType = ",,dataType = "dataType = "UserVO")
    @PostMapping("/create")
    public Result create(@RequestBody UserVO object, BindingResult bindingResult) {
        Result errorList = getResult(bindingResult);
        if (errorList != null) {
            return errorList;
        }
        return userService.create(object);
    }

    @Transactional
    @ApiOperation(value = "根据User对象编辑用户")
//    @ApiImplicitParam(name = "user", value = "用户详细实体UserVO", required = true,paramType = ",,dataType = "dataType = "UserVO")
    @PostMapping("/save")
    public Result save(@RequestBody UserVO user, BindingResult bindingResult) {

        Result errorList = getResult(bindingResult);
        if (errorList != null) {
            return errorList;
        }
        return userService.save(user);
    }

    @ApiOperation(value = "根据id删除用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody Integer id) {
        return userService.delete(id);
    }

    @ApiOperation(value = "根据id字符串批量删除用户")
    @ApiImplicitParam(name = "ids", value = "ids字符串", required = true, dataType = "int")
    @DeleteMapping("/deleteBatch")
    public Result deleteBatch(@RequestBody String ids) {
        return userService.deleteBatch((ConverterUtil.stringConverterToIntegerList(ids)));
    }

    @ApiOperation(value = "根据用户id编辑用户")
    @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "query", dataType = "int")
    @PostMapping("/get")
    public Result get(@RequestParam Integer id) {
        try {
            return userService.get(id);
        } catch (Exception e) {
            throw new EntityNotFoundException(e.getMessage());
        }
    }

    @ApiOperation(value = "关键字查询用户列表")
    @ApiImplicitParam(name = "key", value = "查询关键字", required = true, paramType = "query")
    @GetMapping("/getList")
    public Result getList(@RequestParam String key) {

        return userService.getList(key);

    }

    @ApiOperation(value = "根据关键字分页查询用户")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "查询关键字", required = true, paramType = "query", dataType = "string"),
            @ApiImplicitParam(name = "status", value = "用户状态筛选", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "current", value = "页数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "最大条目数", required = true, paramType = "query", dataType = "int")}
    )
    @GetMapping("/getListPage")
    public Result getListPage(@RequestParam String key, @RequestParam Integer status,
                              @RequestParam Integer current, @RequestParam Integer limit) {
        try {
            return userService.getListPage(key, status, current, limit);
        } catch (Exception e) {
            throw new EntityNotFoundException(e.getMessage());
        }
    }

    @ApiOperation(value = "根据关键字获取弹窗选择列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "key", value = "查询关键字", required = true, paramType = "query"),
            @ApiImplicitParam(name = "current", value = "页数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "limit", value = "最大条目数", required = true, paramType = "query", dataType = "int")
    }
    )
    @GetMapping("/getOptions")
    public Result getOptions(String key, Integer current, Integer limit) {
        try {
            return userService.getOptions(key, current, limit);
        } catch (Exception e) {
            throw new EntityNotFoundException(e.getMessage());
        }
    }

    @ApiOperation(value = "批量审查用户")
    @ApiImplicitParam(name = "ids", value = "用户ids", required = true)
    @PostMapping("/audit")
    public Result audit(@RequestBody String ids) {
        try {
            return userService.audit(ConverterUtil.stringConverterToIntegerList(ids));
        } catch (Exception e) {
            throw new BaseException(ResultEnum.UPDATE_FAIL);
        }
    }

    @ApiOperation(value = "用户修改密码")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "用户id", required = true, dataType = "int"),
            @ApiImplicitParam(name = "password", value = "密码", required = true)}
    )
    @PostMapping("/changePassword")
    public Result changePassword(@RequestBody Integer id, @RequestBody String password) {
        return userService.changePassword(id, password);
    }

}

